﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Plumbing.Properties;

namespace YArchitech.Plumbing
{
	public partial class FormTextMark : System.Windows.Forms.Form
	{
		public FormTextMark(bool isBox, IntPtr hwnd)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			base.Location = new System.Drawing.Point(100, 100);
			this.tmhwnd = hwnd;
			if (isBox)
			{
				this.pictureBox1.Image = YArchitech.Plumbing.Properties.Resources.文字标注_box_;
				this.label2.Text = "平线长(mm)：";
				this.label3.Text = "折线长(mm)：";
				return;
			}
			this.pictureBox1.Image = YArchitech.Plumbing.Properties.Resources.textmark;
		}

		private void FormTextMark_Load(object sender, EventArgs e)
		{
			string[] textMarkText = this.userSetting.GetTextMarkText();
			try
			{
				this.tbMarkText.Text = textMarkText[0];
				this.hyTextBox45Length.Text = textMarkText[1];
				this.hyTextBox90Length.Text = textMarkText[2];
			}
			catch
			{
				this.tbMarkText.Text = "文字标注";
				this.hyTextBox45Length.Text = "1000";
				this.hyTextBox90Length.Text = "1000";
			}
		}

		private void FormTextMark_Deactivate(object sender, EventArgs e)
		{
			if (this.tbMarkText.Text.Trim() != "" && this.hyTextBox45Length.Text != "" && this.hyTextBox90Length.Text != "")
			{
				string[] array = new string[3];
				string text = this.tbMarkText.Text;
				array[0] = text;
				array[1] = this.hyTextBox45Length.Text;
				array[2] = this.hyTextBox90Length.Text;
				this.userSetting.WriteTextMarkText(array);
				return;
			}
			YJKMessageBox.Show("请确保输入不为空！");
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void FormTextMark_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Escape)
			{
				base.Close();
			}
		}

		private void FormTextMark_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			int wMsg = 7;
			FormTextMark.SendMessage(this.tmhwnd, wMsg, IntPtr.Zero, IntPtr.Zero);
			FormTextMark.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormTextMark.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void FormTextMark_FormClosing(object sender, FormClosingEventArgs e)
		{
		}

		private void FormTextMark_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormTextMark.SendMessage(this.tmhwnd, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void FormTextMark_Leave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(Cursor.Position))
			{
				int wMsg = 7;
				FormTextMark.SendMessage(this.tmhwnd, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private List<string> list = new List<string>();

		private IntPtr tmhwnd;
	}
}
